﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace TestForm
{
    public partial class Form2 : Form
    {
        Weather.Base.IWeatherProvider provider = null;
        System.Globalization.CultureInfo ci;
        public Form2()
        {
            InitializeComponent();
            provider = new OpenWeatherMap.WeatherProvider();
            cInfo.DataSource = langList.OpenWeatherMap;
            //Home.Base.GeneralHelper.Proxy = new System.Net.WebProxy("208.64.176.157", 80);

            Time1.Text = Time2.Text = String.Empty;
            ci = new System.Globalization.CultureInfo("ru-RU");
            TScale.DataSource = Enum.GetNames(typeof(Weather.Base.TemperatureScale));
            WScale.DataSource = Enum.GetNames(typeof(Weather.Base.WindSpeedScale));
            //List<F1> fl = new List<F1>();
            //fl.Add(new F1() { ld = new Weather.Base.LocationData() { City = "москва", Code = "gismeteo|4368" } });
            //locList.DataSource = fl;
        }

        private void Search_Click(object sender, EventArgs e)
        {
            Assembly a =Assembly.GetAssembly(provider.GetType());
            //Assembly.GetAssembly(provider.GetType()).Location
            //new AssemblyName(Assembly.GetExecutingAssembly().FullName).Version
            ci = (cInfo.SelectedItem as culInfo).ci;
            if (provider != null)
            {
                DateTime start = DateTime.Now;
                List<Weather.Base.LocationData> res = provider.GetLocations(Query.Text, ci);
                List<F1> fl = new List<F1>();
                if (res != null)
                    foreach (Weather.Base.LocationData ld in res)
                        fl.Add(new F1() { ld = ld });
                locList.DataSource = fl;
                Time1.Text = (DateTime.Now - start).ToString();
            }
        }

        private void GetData_Click(object sender, EventArgs e)
        {
            ci = (cInfo.SelectedItem as culInfo).ci;
            if (provider != null)
                if (locList.SelectedItem != null && locList.SelectedItem is F1)
                {
                    DateTime start = DateTime.Now;
                    Weather.Base.LocationData ld = ((F1)locList.SelectedItem).ld;
                    Weather.Base.WeatherData res = provider.GetWeatherReport(ci, ld, (Weather.Base.TemperatureScale)Enum.Parse(typeof(Weather.Base.TemperatureScale), TScale.SelectedValue as String), (Weather.Base.WindSpeedScale)Enum.Parse(typeof(Weather.Base.WindSpeedScale), WScale.SelectedValue as String), TimeSpan.Zero);
                    result.Text = Newtonsoft.Json.JsonConvert.SerializeObject(res);
                    Time2.Text = (DateTime.Now - start).ToString();
                }
                else
                {
                    DateTime start = DateTime.Now;
                    Weather.Base.LocationData ld = new Weather.Base.LocationData() { Code = "" };
                    Weather.Base.WeatherData res = provider.GetWeatherReport(ci, ld, (Weather.Base.TemperatureScale)Enum.Parse(typeof(Weather.Base.TemperatureScale), TScale.SelectedValue as String), (Weather.Base.WindSpeedScale)Enum.Parse(typeof(Weather.Base.WindSpeedScale), WScale.SelectedValue as String), TimeSpan.Zero);
                    result.Text = Newtonsoft.Json.JsonConvert.SerializeObject(res);
                    Time2.Text = (DateTime.Now - start).ToString();
                }
        }
    }
}
